<?php
/************************************************************************
 Omnitrix - PHP Development Framework
 Copyright (C) 2012-2018 amonest(eastson@outlook.com)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
************************************************************************/

function otx_cache_fetch($key)
{
// 	if (DEBUG) {
// 		return false;
// 	}

	$cache_expire = otx_config_int('system_cache_lifetime') * 60;
	$cache_files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');

	if ($cache_files) {
		foreach ($cache_files as $file_name) {
			$file_time = substr(strrchr($file_name, '.'), 1);
			if ($file_time + $cache_expire >= $GLOBALS['_XTIMESTAMP'] ||
				(func_num_args() > 1 && func_get_arg(1) === true)) {
				if (file_exists($file_name)) {
					$value = file_get_contents($file_name);
					return unserialize($value);
				}
			}
		}
	}

	return false;
}

function otx_cache_write($key, $value)
{
	otx_cache_delete($key);

	$file = DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.' . time();
	$handle = fopen($file, 'w');
	fwrite($handle, serialize($value));
	fclose($handle);
}

function otx_cache_delete($key)
{
	$cache_files = glob(DIR_CACHE . 'cache.' . preg_replace('/[^A-Z0-9\._-]/i', '', $key) . '.*');

	if ($cache_files) {
		foreach ($cache_files as $file_name) {
			if (file_exists($file_name)) {
				unlink($file_name);
				clearstatcache();
			}
		}
	}
}

function otx_cache_purge()
{
	$cache_expire = otx_config_int('system_cache_lifetime') * 60;
	$cache_files = glob(DIR_CACHE . 'cache.*');

	if ($cache_files) {
		foreach ($cache_files as $file_name) {
			$file_time = substr(strrchr($file_name, '.'), 1);
			if ($file_time + $cache_expire < $GLOBALS['_XTIMESTAMP']) {
				if (file_exists($file_name)) {
					unlink($file_name);
					clearstatcache();
				}
			}
		}
	}
}

function otx_cache_list()
{
	$cache_list = array();
	$cache_files = glob(DIR_CACHE . 'cache.*');
	$cache_expire = otx_config_int('system_cache_lifetime') * 60;

	if ($cache_files) {
		foreach ($cache_files as $file_name) {
			$file_time = substr(strrchr($file_name, '.'), 1);
			if ($file_time + $cache_expire < $GLOBALS['_XTIMESTAMP']) {
				if (file_exists($file_name)) {
					unlink($file_name);
					clearstatcache();
				}
			} else {
				$cache_list[] = array(
					'name' => otx_string_trim(basename($file_name), 'cache.', '.' . $file_time),
					'time' => $file_time,
					'size' => filesize($file_name),
					'file' => $file_name
				);
			}
		}
	}

	return $cache_list;
}

function otx_cache_query($key, $sql, $params = array(), $db = null)
{
	$value = otx_cache_fetch($key);
	if (false !== $value) {
		return $value;
	}

	$value = otx_db_fetch_all($sql, $params, $db);
	otx_cache_write($key, $value);

	return $value;
}